#include "Solution.h"

Solution::Solution() {
	size = 0;
	cols = 0;
	rows = 0;
	value = NULL;
	ready = false;
}

Solution::~Solution() {
	if (value != NULL)
		delete [] value;
}

bool Solution::create( Problem *problem ) {
	return create( problem->getInstance() );
}

bool Solution::create( Instance *inst ) {
	size = inst->getSolSize();
	cols = inst->getSolCols();
	rows = inst->getSolRows();
	value = new int[size];
	return true;
}

int& Solution::operator [] ( int i ) throw (InvalidIndex, NotReady) {
	if (!ready)
		throw NotReady("Solution");
	else if (i < 0 || i >= size)
		throw InvalidIndex("i", 0, size - 1, i);

	return value[i];
}

int Solution::get ( int i, int j ) throw (InvalidIndex, NotReady) {
	if (!ready)
		throw NotReady("Solution");
	else if (i < 0 || i >= rows)
		throw InvalidIndex("i", 0, rows- 1, i);
	else if (j < 0 || j >= cols)
		throw InvalidIndex("j", 0, cols - 1, i);

	return value[i*cols + j];
}

void Solution::set ( int i, int j, int x ) throw (InvalidIndex, NotReady) {
	if (!ready)
		throw NotReady("Solution");
	else if (i < 0 || i >= rows)
		throw InvalidIndex("i", 0, rows- 1, i);
	else if (j < 0 || j >= cols)
		throw InvalidIndex("j", 0, cols - 1, i);

	value[i*cols + j] = x;
}